<?php

class Mailer
{
    const SENT_SUCCESSFUL = TRUE;
    const SENDING_FAILED = FALSE;

    private static function sendMail($to, $subject, $content)
    {
        $mailer = new PHPMailer(TRUE);
        self::loadSettings($mailer);

        $mailer->AddAddress($to);
        $mailer->Subject = $subject;
        $mailer->MsgHTML($content);
        $mailer->CharSet = "UTF-8";

        try
        {
            $mailer->Send();
        }
        catch (phpmailerException $e)
        {
            return self::SENDING_FAILED;
        }

        return self::SENT_SUCCESSFUL;
    }

    private static function loadSettings(PHPMailer $mailer)
    {
        $mailer->IsSMTP();

        $mailer->Host = MAIL_HOST;
        $mailer->SMTPDebug = FALSE;
        $mailer->SMTPAuth = TRUE;
        $mailer->Port = MAIL_PORT;
        $mailer->Username = MAIL_USERNAME;
        $mailer->Password = MAIL_PASS;
        $mailer->SMTPSecure = MAIL_SMTP_SECURE;
        $mailer->AddReplyTo(MAIL_FROM_NAME, MAIL_FROM_NAME);
        $mailer->SetFrom(MAIL_FROM_NAME, MAIL_FROM_NAME);
    }

    public static function sendRequest($name, $email, $phone, $coaching, $wantToEnter, $keepInformed, $message)
    {
        $mailBody = @file_get_contents(REQUEST_MAIL_BODY_TPL);

        $mailBody = str_replace('%NAME%', $name, $mailBody);
        $mailBody = str_replace('%EMAIL%', $email, $mailBody);
        $mailBody = str_replace('%PHONE%', $phone, $mailBody);
        $mailBody = str_replace('%COACHING%', $coaching, $mailBody);
        $mailBody = str_replace('%WANT_TO_ENTER%', $wantToEnter, $mailBody);
        $mailBody = str_replace('%KEEP_INFORMED%', $keepInformed, $mailBody);

        $mailBody = str_replace('%MESSAGE%', $message, $mailBody);

        $date = date('m/d/Y H:i:s a', time());
        $mailBody = str_replace('%DATE%', $date, $mailBody);

        $to            = REQUEST_MAIL_BOX;
        $subject       = MAIL_SUBJECT;
        $subject       = str_replace('%NAME%', $name, $subject);

        return Mailer::sendMail($to, $subject, $mailBody);
    }
}

?>